<?php

Raise::load('core.helper.convert.RaiseConvert');

/**
 * TestCoreRaiseConvert class
 * Testing unit of 'phpraise/core/RaiseConvert.php'
 *
 * @author Sam-Mauris Yong / hellclanner at live dot com
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package Raise.UnitTest.Test
 */
class TestCoreRaiseConvert extends RaiseTest {

    public static function testClass() {
        self::assertClassExists('RaiseConvert');
        self::assertClassExtends('RaiseConvert', 'RaiseStaticClass');
        self::assertClassMethod('RaiseConvert', 'to');
        self::assertConstantDefined(array(
            'RaiseConvert::TO_ARRAY'
        ));
    }

    public static function testToString() {
        self::assertStrictEqual(RaiseConvert::to(RaiseConvert::TO_STRING, 'hi'), 'hi');
        self::assertStrictEqual(RaiseConvert::to(RaiseConvert::TO_STRING, 5), '5');
        self::assertStrictEqual(RaiseConvert::to(RaiseConvert::TO_STRING, 5.6), '5.6');
    }

    public static function testToBoolean() {
        self::assertStrictEqual(RaiseConvert::to(RaiseConvert::TO_BOOLEAN, 'hi'), true);
        self::assertStrictEqual(RaiseConvert::to(RaiseConvert::TO_BOOLEAN, ''), false);
        self::assertStrictEqual(RaiseConvert::to(RaiseConvert::TO_BOOLEAN, 0), false);
        self::assertStrictEqual(RaiseConvert::to(RaiseConvert::TO_BOOLEAN, null), false);
        self::assertStrictEqual(RaiseConvert::to(RaiseConvert::TO_BOOLEAN, 5), true);
    }

    public static function testToInteger() {
        self::assertStrictEqual(RaiseConvert::to(RaiseConvert::TO_INTEGER, 'hi'), 0);
        self::assertStrictEqual(RaiseConvert::to(RaiseConvert::TO_INTEGER, '6hi'), 6);
        self::assertStrictEqual(RaiseConvert::to(RaiseConvert::TO_INTEGER, 5), 5);
        self::assertStrictEqual(RaiseConvert::to(RaiseConvert::TO_INTEGER, 5.6), 5);
    }

    public static function testToDouble() {
        self::assertStrictEqual(RaiseConvert::to(RaiseConvert::TO_DOUBLE, '5.7'), 5.7);
        self::assertStrictEqual(RaiseConvert::to(RaiseConvert::TO_DOUBLE, '6'), 6.0);
        self::assertStrictEqual(RaiseConvert::to(RaiseConvert::TO_DOUBLE, 5), 5.0);
    }

    public static function testToArray() {
        $c = new stdClass();
        $c->test = 50;
        self::assertStrictEqual(RaiseConvert::to(RaiseConvert::TO_ARRAY, $c), array('test' => 50));
    }

    public static function testToObject() {
        $c = new stdClass();
        $c->test = 50;
        self::assertEqual(RaiseConvert::to(RaiseConvert::TO_OBJECT, array('test' => 50))->test, $c->test);
        self::assertEqual(RaiseConvert::to(RaiseConvert::TO_OBJECT, array('test' => 50)), $c);
    }

    public static function testToDateTime() {
        self::assertObjectClass(RaiseConvert::to(RaiseConvert::TO_DATETIME, 'Thu, 21 Dec 2000 16:01:07 +0200'), 'RaiseDateTime');
        self::assertStrictEqual(RaiseConvert::to(RaiseConvert::TO_DATETIME, 'Thu, 21 Dec 2000 16:01:07 +0200')->date()->year(), 2000);
        self::assertObjectClass(RaiseConvert::to(RaiseConvert::TO_DATETIME, 1306479751), 'RaiseDateTime');
        self::assertStrictEqual(RaiseConvert::to(RaiseConvert::TO_DATETIME, 1306479751)->date()->year(), 2011);
        self::assertStrictEqual(RaiseConvert::to(RaiseConvert::TO_DATETIME, 1306479751)->date()->month(), 5);
        self::assertObjectClass(RaiseConvert::to(RaiseConvert::TO_DATETIME, null), 'RaiseDateTime');
        self::assertStrictEqual(RaiseConvert::to(RaiseConvert::TO_DATETIME, null)->date()->year(), RaiseDateTime::now()->date()->year());
        self::assertStrictEqual(RaiseConvert::to(RaiseConvert::TO_DATETIME, null)->date()->month(), RaiseDateTime::now()->date()->month());
    }

    public static function testToPoint() {
        $p = new RaisePoint(50, 20);
        self::assertObjectClass(RaiseConvert::to(RaiseConvert::TO_POINT, new RaiseRectangle($p, 100, 20)), 'RaisePoint');
        self::assertStrictEqual(RaiseConvert::to(RaiseConvert::TO_POINT, new RaiseRectangle($p, 100, 20)), $p);
        self::assertStrictEqual(RaiseConvert::to(RaiseConvert::TO_POINT, new RaiseCollection(50, 20))->x(), $p->x());
        self::assertStrictEqual(RaiseConvert::to(RaiseConvert::TO_POINT, new RaiseCollection(50, 20))->y(), $p->y());
        self::assertStrictEqual(RaiseConvert::to(RaiseConvert::TO_POINT, '50, 20')->x(), (double) $p->x());
        self::assertStrictEqual(RaiseConvert::to(RaiseConvert::TO_POINT, '50,20')->y(), (double) $p->y());
    }

    public static function testToRectangle() {
        self::assertObjectClass(RaiseConvert::to(RaiseConvert::TO_RECTANGLE, '20,30,50,10'), 'RaiseRectangle');
        self::assertStrictEqual(RaiseConvert::to(RaiseConvert::TO_RECTANGLE, '20,30,50,10')->x(), 20.0);
        self::assertStrictEqual(RaiseConvert::to(RaiseConvert::TO_RECTANGLE, '20,30,50,10')->y(), 30.0);
        self::assertStrictEqual(RaiseConvert::to(RaiseConvert::TO_RECTANGLE, '20,30,50,10')->x2(), 70.0);
        self::assertStrictEqual(RaiseConvert::to(RaiseConvert::TO_RECTANGLE, '20,30,50,10')->y2(), 40.0);
        self::assertStrictEqual(RaiseConvert::to(RaiseConvert::TO_RECTANGLE, array(20, 30, 50, 10))->x(), 20.0);
    }

}